package StudyCode.summary.regularexpressionsmatch;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class CountsManyTimesTheRegexpAppears {
	public static void main(String[] argv) {
		String str = "www.tah1986.com";
		String regexp = "www.tah1986.com";
		System.out.println(countRegexpMatches(str, regexp));
	}

	public static int countRegexpMatches(String str, String regexp) {
		if (isEmpty(str) || isEmpty(regexp)) {
			return 0;
		}
		Pattern pattern = Pattern.compile(regexp);
		Matcher matcher = pattern.matcher(str);
		int i = 0;
		while (matcher.find()) {
			i++;
		}
		return i;
	}


	public static boolean isEmpty(String str) {
		return str == null || str.length() == 0;
	}

	public static int length(String str) {
		return str == null ? 0 : str.length();
	}
}
